package com.qj.yantaishan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qj.yantaishan.common.utils.DateUtils;
import com.qj.yantaishan.domain.OrderPackage;
import com.qj.yantaishan.mapper.OrderVoMapper;
import com.qj.yantaishan.mapper.ReturnMapper;
import com.qj.yantaishan.service.IOrderPackageService;
import com.qj.yantaishan.service.IOrderVoService;
import com.qj.yantaishan.vo.FtOrderVo;
import com.qj.yantaishan.vo.OrderProductVo;
import com.qj.yantaishan.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Primary
public class IOrderVoServiceImpl extends ServiceImpl<OrderVoMapper, OrderVo> implements IOrderVoService {
    @Autowired
    private IOrderPackageService packageService;
    @Autowired
    private ReturnMapper returnMapper;
    @Override
    public OrderVo getOrderDetail(Integer orderId) {
        OrderVoMapper orderVoMapper = getBaseMapper();
        OrderVo orderVo = getById(orderId);
        List<OrderProductVo> orderProductVos = orderVoMapper.selectOrderProductListByOrderId(orderId);
        orderVo.setOrderProductVos(orderProductVos);
        OrderPackage orderPackage = packageService.getOne(new LambdaQueryWrapper<OrderPackage>().eq(OrderPackage::getOrderId, orderId));
        orderVo.setOrderPackage(orderPackage);
        List<Map<String, Object>> mapList = returnMapper.selectReturnProduct(orderId);
        orderVo.setReturnProductList(mapList);
        return orderVo;
    }

    @Override
    public Map<String, Object> getFtOrderList(Integer currentPage, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        List<FtOrderVo> ftOrderVos = baseMapper.selectFtOrderPage(currentPage, pageSize);
        Integer total = baseMapper.ftOrderTotal();
        map.put("current", currentPage);
        map.put("total", total);
        map.put("size", pageSize);
        map.put("pages", ((total + pageSize) - 1) / pageSize);
        map.put("records", ftOrderVos);
        return map;
//        return null;
    }

    @Override
    public FtOrderVo getFtOrderDetail(Integer orderId) {
        return baseMapper.getFtOrderDetail(orderId);
//        return null;
    }

    @Override
    public int saveFtOrder(String certPath, String videoPath, String orderId) {
        int orderId1 = Integer.parseInt(orderId);
        //封坛证书 有些订单是没的    这里查询是否已经拥有封坛证书
        int ftOrderWine = baseMapper.getFtOrderWine(orderId1);
        if (ftOrderWine == 0) {
            //没有封坛证书则为新增
            return baseMapper.insertFtOrderDetail(certPath, videoPath, orderId);
        }
        //有封坛证书则为修改
        return baseMapper.saveFtOrder(certPath, videoPath, orderId);

    }

    @Override
    public Map<String, Object> getOrderOverview() {
        //1.7天下单笔数  604800七天的秒数
        //1.1获取当前时间
        Long nowTime = new Date().getTime() / 1000;
        //1.2七天前
        Long before7Time = nowTime - 604800;
        int orderNumber = baseMapper.get7DayOrderNumber(nowTime, before7Time);
        //2.代付款
        int orderNoPayNumber = baseMapper.getNoPayOrderNum();
        //3.代发货
        int orderNoShipNumber = baseMapper.getNoShipNumber();
        //4.已发货
        int orderShipNumber = baseMapper.getShopNumber();
        //5.七天收入
        Double order7DayPay = baseMapper.getOrder7DayPayMoney(nowTime, before7Time);
        //6.总销售额
        Double totalPayMoney = baseMapper.getOrderTotalMoney();
        //7天订单趋势
        List<Map<String, Object>> order7Trend = new ArrayList<>();
        //当前时间
        Date nowDate = DateUtils.getNowDate();
        for (int i = 0; i < 7; i++) {
            Map<String, Object> map = new HashMap<>();
            Calendar cal = Calendar.getInstance();
            cal.setTime(nowDate);
            //循环7次获取七天的时间挫 查询对应的订单
            cal.add(Calendar.DAY_OF_MONTH, -i);
            Date timeStart = cal.getTime();

            Date timeEmd = cal.getTime();

            timeStart.setHours(00);
            timeStart.setMinutes(00);
            timeStart.setSeconds(00);
            long startTime = timeStart.getTime()/1000;
            timeEmd.setHours(23);
            timeEmd.setMinutes(59);
            timeEmd.setSeconds(59);
            long endTime = timeEmd.getTime()/1000;
            int nowDayShipNumber = baseMapper.getNowDayShip(startTime, endTime);
            int nowDayPayNumber = baseMapper.getNowDayPay(startTime, endTime);
            int nowDayOrderNumber = baseMapper.getNowDayOrder(startTime, endTime);
            final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            final String format = dateFormat.format(timeStart);

            map.put("date",format);
            map.put("nowDayShipNumber", nowDayShipNumber);
            map.put("nowDayPayNumber", nowDayPayNumber);
            map.put("nowDayOrderNumber", nowDayOrderNumber);
            order7Trend.add(map);
        }


        Map<String, Object> map = new HashMap<>();
        //昨日发货
        map.put("yesterdayOrderNumber",order7Trend.get(1).get("nowDayOrderNumber"));
        //昨日付款
        map.put("nowDayPayNumber",order7Trend.get(1).get("nowDayPayNumber"));
        //昨日发货
        map.put("nowDayShipNumber",order7Trend.get(1).get("nowDayShipNumber"));
        map.put("orderNumber", orderNumber);
        map.put("orderNoPayNumber", orderNoPayNumber);
        map.put("orderNoShipNumber", orderNoShipNumber);
        map.put("orderShipNumber", orderShipNumber);
        map.put("order7DayPayMoney", order7DayPay);
        map.put("totalPayMoney", totalPayMoney);
        map.put("order7Trend", order7Trend);

        return map;
    }
}
